package boboUtil;

import java.util.Objects;

public abstract class BoboAbstractSet<E> extends BoboAbstractCollection<E> implements BoboSet<E> {
    protected BoboAbstractSet(){
    }

    public boolean equals(Object o){
        if (o == this){
            return true;
        }

        if (!(o instanceof BoboSet)){
            return false;
        }

        BoboCollection<?> c = (BoboCollection<?>) o;
        if (c.size() != size()){
            return false;
        }
        try {
            return containsAll(c);
        }catch (ClassCastException unused)   {
            return false;
        } catch (NullPointerException unused) {
            return false;
        }
    }

    public int hashCode(){
        int h = 0;
        BoboIterator<E> it = iterator();
        while (it.hasNext()){
            E obj = it.next();
            if (obj != null)
                h += obj.hashCode();
        }
        return h;
    }

    public boolean removeAll(BoboCollection<?> c){
        Objects.requireNonNull(c);
        boolean modified = false;


        if (size() > c.size()){
            for (BoboIterator<?> i = c.iterator(); i.hasNext(); ){
                modified |= remove(i.next());
            }
        } else {
            for (BoboIterator<?> i = iterator(); i.hasNext(); ){
                if (c.contains(i.next())){
                    i.remove();
                    modified = true;
                }
            }
        }
        return modified;
    }
}
